package com.seguriapp.gwt.server;

import net.customware.gwt.dispatch.server.ExecutionContext;
import net.customware.gwt.dispatch.shared.DispatchException;

import org.apache.log4j.Logger;

import com.seguriapp.common.gwt.server.AbstractSeguriAppHandler;
import com.seguriapp.common.security.SeguriAppSession;
import com.seguriapp.common.security.SeguriAppSession.Key;
import com.seguriapp.common.security.SeguriAppUser;
import com.seguriapp.entities.User;
import com.seguriapp.gwt.shared.LogOffUserAction;
import com.seguriapp.gwt.shared.LogOffUserResult;

public class LogOffUserHandler extends AbstractSeguriAppHandler<LogOffUserAction, LogOffUserResult> {

  private static final Logger LOGGER = Logger.getLogger(LogOffUserHandler.class);

  public Class<LogOffUserAction> getActionType() {
    return LogOffUserAction.class;
  }

  public LogOffUserResult execute(final LogOffUserAction action, final ExecutionContext context) throws DispatchException {
    final LogOffUserResult result = new LogOffUserResult();
    SeguriAppSession session = SeguriAppUser.getSeguriAppSecureUser().getSeguriAppSession();
    final User user = (User) session.get(Key.User);
    if (user != null) {
      session.remove(Key.User);
      session.remove(Key.MenuLinks);
      LOGGER.info("Logginf Off user: " + user.getUsername());
    }
    return result;
  }

}
